package com.yanqu.road.dao.impl.activity.hongxian;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.hongxian.config.HongXianImageInfo;
import com.yanqu.road.entity.activity.hongxian.config.HongXianMemoryInfo;
import com.yanqu.road.entity.activity.hongxian.config.HongXianRewardInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.*;

public class HongXiuConfigDaoImpl extends TempDao {

    private static String getHongXianMemoryInfoSql = "select * from `t_s_activity_hongxian_memory` where `activityId`=? ORDER BY `id` ASC";
    public List<HongXianMemoryInfo> getHongXianMemoryInfoList(int activityId) {
        List<HongXianMemoryInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getHongXianMemoryInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HongXianMemoryInfo data = new HongXianMemoryInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setName(rs.getString("name"));
                data.setMemoryLevel(rs.getInt("memoryLevel"));
                data.setText(rs.getString("text"));
                data.setFristWeight(rs.getInt("fristWeight"));
                data.setSecondWeight(rs.getInt("secondWeight"));
                data.setLeastNum(rs.getString("leastNum"));
                data.setSpecialNum(rs.getString("specialNum"));
                data.setNormalNum(rs.getString("normalNum"));
                data.setScore(rs.getInt("score"));
                data.setEffect(rs.getString("effect"));
                data.setEffectMap(StringUtils.stringToIntegerList(data.getEffect(), "\\|"));
                if(!StringUtils.isNullOrEmpty(data.getLeastNum()) &&
                    !"0".equals(data.getLeastNum()) && !"0;0".equals(data.getLeastNum())){
                    data.setLeastNumMap(StringUtils.stringToIntegerList(data.getLeastNum(), ";"));
                }
                if(!StringUtils.isNullOrEmpty(data.getSpecialNum()) &&
                        !"0".equals(data.getSpecialNum()) && !"0;0".equals(data.getSpecialNum())){
                    data.setSpecialNumMap(StringUtils.stringToIntegerList(data.getSpecialNum(), ";"));
                }
                if(!StringUtils.isNullOrEmpty(data.getNormalNum()) &&
                        !"0".equals(data.getNormalNum()) && !"0;0".equals(data.getNormalNum())){
                    data.setNormalNumMap(StringUtils.stringToIntegerList(data.getNormalNum(), ";"));
                }
                list.add(data) ;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getHongXianRewardInfoSql = "select * from `t_s_activity_hongxian_reward` where `activityId`=? ORDER BY `id` ASC";
    public List<HongXianRewardInfo> getHongXianRewardInfoList(int activityId) {
        List<HongXianRewardInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getHongXianRewardInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HongXianRewardInfo data = new HongXianRewardInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setName(rs.getString("name"));
                data.setType(rs.getInt("type"));
                data.setAward(rs.getString("award"));
                data.setLevel(rs.getInt("level"));
                data.setNotice(rs.getInt("notice"));
                if(data.getType() != 3){
                    Map<String, Integer> awardMap = new HashMap<>();
                    List<String> stringList = StringUtils.stringToStringList(data.getAward(), "\\|");
                    for (String s : stringList) {
                        List<String> string = StringUtils.stringToStringList(s, ",");
                        awardMap.put(string.get(0), Integer.parseInt(string.get(1)));
                    }
                    data.setAwardMap(awardMap);
                }else {
                    data.setAwardMap(Collections.emptyMap());
                }
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getHongXianImageInfoSql = "select * from `t_s_activity_hongxian_image` where `activityId`=? ORDER BY `id` ASC";
    public List<HongXianImageInfo> getHongXianImageInfoList(int activityId) {
        List<HongXianImageInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getHongXianImageInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HongXianImageInfo data = new HongXianImageInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setImageId(rs.getString("imageID"));
                data.setType(rs.getInt("type"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

}
